﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Order.CAInterface;

namespace CYTS.Aoyou.Order.CacheAccess
{
    public class OrderInfoCacheAdapterForRedis: CacheAdapterBase, IOrderInfoCacheAdapter, ICacheAdapter
    {
        public OrderInfoCacheAdapterForRedis(CacheProvider provider)
            : base(provider)
        { }

        public Entity.OrderInfo GetOrderInfo(int orderID)
        {
            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("BussinessCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];

            Entity.OrderInfo orderInfo = null;
            try
            {
                RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
                orderInfo = helper.Get<Entity.OrderInfo>(orderID.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("从缓存获取数据报错", ex);
            }

            return orderInfo;
        }

        public void PutOrderInfo(Entity.OrderInfo orderInfo)
        {
            if (orderInfo == null)
            {
                return;
            }

            List<CacheConnection> connectionList = null;
            if (!this.DicCacheConnection.TryGetValue("BussinessCache", out connectionList) || connectionList.Count == 0)
            {
                throw new Exception("未找到对应的缓存连接串");
            }

            CacheConnection connection = connectionList[0];

            try
            {
                RedisHelper helper = new RedisHelper(connection.Host, connection.Port, Convert.ToInt32(connection.RegionName), connection.Password, this.Region);
                helper.Add(orderInfo.OrderID.ToString(), orderInfo, new TimeSpan(0, 5, 0));
            }
            catch (Exception ex)
            {
                throw new Exception("缓存增加数据报错", ex);
            }
        }
    }
}
